package com.atguigu.test;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Minio {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //
        MinioClient minioClient = MinioClient.builder().
                endpoint("http://192.168.6.100:9000").credentials("minioadmin", "minioadmin").build();
        //创建bucket
        boolean b = minioClient.bucketExists(BucketExistsArgs.builder().bucket("0625").build());
        if (!b) {
            //创建hello-minio桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("hello-minio").build());
            //设置hello-minio桶的访问权限
            String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::0625/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""";
            //把resource改了就行，后面是桶名
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket("0625").config(policy).build());
        } else {
            System.out.println("Bucket '0625' already exists.");
        }

        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket("0625")
                        .object("AA.jpg")
                        .filename("E:\\Code\\JavaWeb_code\\springboot1234\\minio-test\\src\\main\\resources\\AA.jpg")
                        .build());
        System.out.println("上传成功");
    }
}


